package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.AcceptBo;
import com.ruoyi.system.domain.vo.AcceptVo;
import com.ruoyi.system.domain.Accept;
import com.ruoyi.system.mapper.AcceptMapper;
import com.ruoyi.system.service.IAcceptService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 接收Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-02
 */
@RequiredArgsConstructor
@Service
public class AcceptServiceImpl implements IAcceptService {

    private final AcceptMapper baseMapper;
    private final SysUserMapper sysUserMapper;

    /**
     * 查询接收
     */
    @Override
    public AcceptVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询接收列表
     */
    @Override
    public TableDataInfo<AcceptVo> queryPageList(AcceptBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Accept> lqw = buildQueryWrapper(bo);
        Page<AcceptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(x->{
            SysUser sysUser = sysUserMapper.selectUserById(x.getUserId());
            if(sysUser!=null) {
                x.setUserName(sysUser.getNickName());
            }else {
                x.setUserName("游客");
            }
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询接收列表
     */
    @Override
    public List<AcceptVo> queryList(AcceptBo bo) {
        LambdaQueryWrapper<Accept> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Accept> buildQueryWrapper(AcceptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Accept> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, Accept::getUserId, bo.getUserId());
        lqw.eq(bo.getShareId() != null, Accept::getShareId, bo.getShareId());
        return lqw;
    }

    /**
     * 新增接收
     */
    @Override
    public Boolean insertByBo(AcceptBo bo) {
        Accept add = BeanUtil.toBean(bo, Accept.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改接收
     */
    @Override
    public Boolean updateByBo(AcceptBo bo) {
        Accept update = BeanUtil.toBean(bo, Accept.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Accept entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除接收
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
